﻿using ApiDoc.DAL;
using ApiDoc.Models;
using ApiDoc.Models.BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ApiDoc.Admin.Server.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class StepController : ControllerBase
    { 
        private readonly FlowStepDAL flowStepDAL;
        private readonly FlowStepParamDAL flowStepParamDAL;

        public StepController(  FlowStepDAL flowStepDAL, 
                            FlowStepParamDAL flowStepParamDAL )
        { 
            this.flowStepDAL = flowStepDAL;
            this.flowStepParamDAL = flowStepParamDAL;
        }

        [HttpGet("Get")]
        public StepModel Get(int SN)
        {
            StepModel model = this.flowStepDAL.Get<StepModel>(SN); 
            model.StepParamList = this.flowStepParamDAL.Query(SN);
            return model;
        }

        [HttpGet("List")]
        public List<FlowStepModel> List(int FKSN)
        {
            List<FlowStepModel> list = this.flowStepDAL.Query(FKSN);

            return list;
        }

        [HttpPost("Save")]
        public int Save(FlowStepModel model)
        {
            int SN = 0;
            if (model.SN > 0)
            {
                FlowStepModel modelOld = this.flowStepDAL.Get<FlowStepModel>(model.SN);
                model.CommandText = modelOld.CommandText;
                model.CommandType = modelOld.CommandType;
                model.DataBase = modelOld.DataBase;
                SN =  this.flowStepDAL.Update(model);
            }
            else
            {
                SN = this.flowStepDAL.Insert(model); 
            }
            return SN;

        }

        [HttpGet("Delete")]
        public int Delete(int SN)
        { 
            int result = this.flowStepDAL.DeleteAll(SN);
            return result;
        }


        [HttpPost("Save/CmdText")]
        public int SaveCmdText(FlowStepModel model)
        {
            int SN = model.SN; 
            string CommandType = model.CommandType;
            string CommandText = model.CommandText;
            string DataBase = model.DataBase;
            string ServiceName = model.ServiceName;
            return this.flowStepDAL.SaveCmdText(SN, CommandType, CommandText, DataBase, ServiceName);
        }


        #region 步骤参数

        [HttpGet("Param/List")]
        public List<FlowStepParamModel> StepParamList(int FKSN)
        {
            List<FlowStepParamModel> list = this.flowStepParamDAL.Query(FKSN);
            return list;
        }

        [HttpPost("Param/Save")]
        public int StepParamSave(FlowStepParamModel model)
        {
            int result = 0;
            if (model.SN > 0)
            {
                result = this.flowStepParamDAL.Update(model);
            }
            else
            {
                result = this.flowStepParamDAL.Insert(model);
            }
            return result;
        }

        [HttpPost("Param/Delete")]
        public int StepParamDelete(List<int> ids)
        {
            int result = 0;
            foreach (int SN in ids)
            {
                FlowStepParamModel model = new FlowStepParamModel();
                model.SN = SN;
                int i = this.flowStepParamDAL.Delete(model);
                result += i;
            }
            return result;
        }

        //[HttpPost]
        //public IActionResult InterParamList(int FKSN, int StepSN)
        //{
        //    List<ParamModel> list = this.paramDAL.Query(FKSN);

        //    List<FlowStepParamModel> listStep = this.flowStepParamDAL.Query(StepSN);
        //    //如果已经存在，不显示

        //    List<ParamModel> listnew = new List<ParamModel>();
        //    foreach (ParamModel param in list)
        //    {
        //        int count = listStep.Where(t => t.ParamName == param.ParamName).Count();
        //        if (count == 0)
        //        {
        //            listnew.Add(param);
        //        }
        //    }
        //    return PartialView("/Views/Interface/ParamSelectList.cshtml", listnew);
        //}

        //[HttpPost]
        //public IActionResult StepParamSaveList(List<FlowStepParamModel> list)
        //{
        //    int fksn = 0;
        //    foreach (FlowStepParamModel model in list)
        //    {
        //        fksn = model.FKSN;
        //        model.IsPreStep = false;
        //        this.flowStepParamDAL.Insert(model);
        //    }

        //    return StepParamList(fksn);
        //}

        #endregion


    }
}
